array-slice-x
Cross-browser array slicer.
module.exports(array, [start], [end])
⇒ Array
⏏
The slice() method returns a shallow copy of a portion of an array into a new
array object selected from begin to end (end not included). The original
array will not be modified.
Kind: Exported function
Returns: Array
- A new array containing the extracted elements.
Param | Type | Description |
---|
array | Array | Object | The array to slice. |
[start] | number | Zero-based index at which to begin extraction. A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence. If begin is undefined, slice begins from index 0. |
[end] | number | Zero-based index before which to end extraction. Slice extracts up to but not including end. For example, slice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3). A negative index can be used, indicating an offset from the end of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence. If end is omitted, slice extracts through the end of the sequence (arr.length). If end is greater than the length of the sequence, slice extracts through the end of the sequence (arr.length). |
Example
import slice from 'array-slice-x';
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
console.log(slice(fruits, 1, 3));